<?php
/*
   帐户余额模型
*/
namespace common\models\finance;
use common\models\BaseModel;
use yii\db\Query;
use common\models\finance\BalanceLogModel;
use common\models\finance\BalanceTypeModel;

class BalanceModel extends BaseModel
{
    public static $typeNameEn = array('1' => '余额' ,'3'=> '合约钱包',4=>'收益钱包');
	public static function tableName()
	{
		return "{{%balance}}";
	}

	public function rules() 
	{
		return [
			[['member_id','balance_type','balance','total_balance'],'required'],
			[['balance','total_balance','use_balance'],'number'],
			[['member_id','mch_id','create_time','update_time'],'integer'],
		];
	}


   	/*
     	获取帐本余额
     	* memberId 会员ID
     	* bType 账本类型
      ifIn 类内部 调用 
   	*/	
    public static function getBalance($memberId,$bType,$ifIn = false)
    {
        $field = "a.balance,a.id,a.balance_type,a.total_balance,a.use_balance,a.member_id,b.type_name,b.is_tixian,b.is_exchange,b.is_transfer,b.is_recharge,b.price";
        $data = self::find()->alias('a')->select($field)
		        ->leftJoin('balance_type as b','a.balance_type=b.type')
		        ->where(['a.member_id'=>$memberId,'a.balance_type'=>$bType,'a.mch_id'=>MCHID])
                ->asarray()->one();

        $lang = \Yii::$app->params['lang'];
        if($ifIn) return $data;

		if($data){
            if($lang == 'en') $data['type_name'] = isset(self::$typeNameEn[$data['balance_type']])?self::$typeNameEn[$data['balance_type']]:$data['type_name'];
            $data['is_detail'] = 1; 
            return $data;
        } 

        ## 给一个默认数据
        $res = BalanceTypeModel::find()->where(['type'=>$bType,'mch_id'=>MCHID])->asarray()->one();
        if(!$res) return $data;
        $data = ['member_id'=>$memberId,'type_name'=>$res['type_name'],'balance_type'=>$res['type'],'balance'=>0];
        $data['is_tixian'] = $res['is_tixian'];
        $data['is_exchange'] = $res['is_exchange'];
        $data['is_transfer'] = $res['is_transfer'];
        $data['is_recharge'] = $res['is_recharge'];
        $data['is_detail'] = 1;
        $data['price'] = $res['price'];
        if($data){
            if($lang == 'en') $data['type_name'] = isset(self::$typeNameEn[$data['balance_type']])?self::$typeNameEn[$data['balance_type']]:$data['type_name'];
            return $data;
        }        
        return $data;       	
    }

    
    /*
       获取所有帐本余额
       * memberId 会员ID
    */  
    public static function getAllBalance($memberId)
    {
        $data = BalanceTypeModel::find()->where(['mch_id'=>MCHID])->asarray()->all();
        $list = array();
        foreach($data as $val)
        {
            $bInfo = self::getBalance($memberId,$val['type']);
            $list[] = $bInfo;
        }
        return $list;         
    }



    /*
        * 增加账本余额
        * mid       会员id
        * btype  账本类型
        * totalFee  金额
        * sType     操作类型  表balance_log的source_type定义
        * sourceId  与类型对应，对应的日志或订单id
        * from      分红的来源 （比如是来自某个人）
        * reback    是否为驳回增加，默认为否      
    */  

    public static function addBalance($mid,$btype,$totalFee,$sType='',$sourceId=0,$from='',$reback=false)
    {
    	  if($totalFee<=0) return array("state"=>false,"msg"=>'金额不能小于0');
    	  $curBalance = self::getBalance($mid,$btype,true);

        if(!$curBalance)
        {
           $res = self::createNewBalance($mid,$btype);

           if(!$res['state']) return array("state"=>false,"msg"=>$res['msg']); 
           $curBalance = $res['cur_balance'];
        } 
        $data = array(
        	'balance'       => $curBalance['balance'] + $totalFee,
           	'total_balance' => $curBalance['total_balance'] + $totalFee,
            'update_time'   => time(),
        );
       	//驳回
       	if($reback){
           	$data['total_balance'] = $curBalance['total_balance'];
           	$data['use_balance']   = $curBalance['use_balance'] - $totalFee;
           	$data['balance']       = $curBalance['balance'] + $totalFee;
       	}
        $where = array("id"=>$curBalance['id']);
        $flag  = self::updateAll($data,$where); 
        if(!$flag) return array("state"=>false,"msg"=>'增加帐本失败'); 
                        	        
       	$note = "+".round($totalFee,4);  
       	$flag  = self::addBalanceLog($curBalance,$totalFee,$sType,1,$sourceId,$from,$note);
       	return array("state"=>true,"msg"=>'增加帐本成功');
    }

    /*
        * 减少余额
        * mid 会员id
        * typeId   会员类型
        * bType    账本类型
        * money    金额
        * sType    操作类型  表balance_log的source_type定义
        * sourceId 与类型对应，对应的日志或订单id
    */

    public static function reduceBalance($mid,$btype,$totalFee,$sType='',$sourceId=0,$from='')
    {
        if($totalFee<=0) return array("state"=>false,"msg"=>'金额不能小于0');

        $curBalance =self::getBalance($mid,$btype,true); 
        if(!$curBalance)
            return array('state'=>0,'msg'=>'余额不足');

        if($curBalance['balance']<$totalFee)
            return array('state'=>0,'msg'=>'余额不足'); 

        $data = array(
            'balance'       => $curBalance['balance'] - $totalFee,
            'use_balance'   => $curBalance['use_balance'] + $totalFee,
            'update_time'   => time(),
        );

        $where = ["id"=>$curBalance['id']];
        $flag  = self::updateAll($data,$where);

        if(!$flag)
            return array("state"=>false,"msg"=>'减少帐本失败');

        $note = "-".round($totalFee,4);

        $flag  = self::addBalanceLog($curBalance,$totalFee,$sType,0,$sourceId,$from,$note);
        var_dump($flag);exit();
        return array("state"=>true,"msg"=>'减少帐本成功');
    }


    /*
        创建一个帐本
    */
    private static function createNewBalance($mid,$btype)
    {
        //有没有创建该帐本类型
        $typeInfo = (new Query)->from('balance_type')->where(["type"=>$btype,'mch_id'=>MCHID])->one();
        if(!$typeInfo) return array("state"=>0,"msg"=>'没有可用帐本');
        $data = array(
           	'member_id'         => $mid,
            'mch_id'            => MCHID,
           	"balance"           => 0,
           	"total_balance"     => 0,
           	"balance_type"      => $btype,
           	"use_balance"       => 0,
           	"update_time"       => time(),
           	"create_time"       => time()
        );
        $model = new self;
        if(!$model->load($data,'') || !$model->save())
        	return self::outError($model->errors);
        $id = \Yii::$app->db->getLastInsertID();
        $data['id'] = $id;
        $data['type_name'] = $typeInfo['type_name'];
        return array('state'=>1,'msg'=>'创建帐本成功！','cur_balance'=>$data);
    }

    /*
		记录日志
    */
    public static function addBalanceLog($curBalance,$totalFee,$sType,$type,$sourceId,$from,$note)
    {
        $data = array(
            'member_id'         => $curBalance['member_id'],
            'mch_id'            => MCHID,
            "balance"           => $totalFee,
            "balance_id"        => $curBalance['id'],
            "balance_type"      => $curBalance['balance_type'],
            "source_type"       => $sType,
            "log_type"          => $type,
            "source_id"         => $sourceId,
            "old_balance"       => $curBalance['balance'],
            "note"              => $note,
            'bonus_from'        => $from,
            "create_time"       => time()
        );
        $model = new BalanceLogModel;
        return ($model->load($data,'')&&$model->save());
    }


    /*
      账本类型
    */
    public static function balanceTypeName($type=0)
    {
        $lang = \Yii::$app->params['lang'];

        if($type>0)
        {
            $data =  BalanceTypeModel::find()->where(['type'=>$type,'mch_id'=>MCHID])->asarray()->one();
            if($data && $lang == 'en'){
                $data['type_name'] = isset(self::$typeNameEn[$data['type']])?self::$typeNameEn[$data['type']]:$data['type_name'];
            }     
            return $data;       
        }

        $typeInfo = BalanceTypeModel::find()->where(['mch_id'=>MCHID])->asarray()->all();
        $data = array();
        foreach($typeInfo as $val)
        {
            $typeName = $val['type_name'];
            if($lang == 'en') $typeName = isset(self::$typeNameEn[$val['type']])?self::$typeNameEn[$val['type']]:$typeName;
            $data[$val['type']] =  $typeName;
        }      
        return $data;
    }
}
